﻿Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Linq
Imports System.ComponentModel

Imports IntoNails.DataAccess
Imports IntoNails.Models
Imports IntoNails.My.Resources
Imports IntoNails.Test.DataGenerator

Namespace ViewModel
	Public Class NailCategoryRepositoryViewModel
		Inherits SubWorkspaceViewModel
		
		#Region "Fields"
		
		
		
		#End Region
		
		#Region "Constructors"
		
		Public Sub New()
			Me.DisplayName = StringResources.INTONAILS_NAILCATEGORYREPOSITORY_VIEWMODEL_DISPLAYNAME
			
			' Subscribe for data notifications.
            ''AddHandler Me.Repository.DataAdded, AddressOf Me.NailCategoryRepository_Added
            ''AddHandler Me.Repository.DataRemoved, AddressOf Me.NailCategoryRepository_Removed
            ''AddHandler Me.Repository.DataUpdated, AddressOf Me.NailCategoryRepository_Updated
			
            Me.PopulateNailCategoryTree()
        End Sub

#End Region

#Region "Properties"

        Private _repository As NailCategoryRepository
        Public ReadOnly Property Repository() As NailCategoryRepository
            Get
                If _repository Is Nothing Then
                    _repository = New NailCategoryRepository(StringResources.NAILCATEGORY_FILE_PATH)
                End If

                Return _repository
            End Get
        End Property

        Private _nailCategoryCollection As ObservableCollection(Of NailCategoryViewModel)
        Public Property RootNailCategoryCollection() As ObservableCollection(Of NailCategoryViewModel)
            Get
                Return _nailCategoryCollection
            End Get
            Private Set(ByVal value As ObservableCollection(Of NailCategoryViewModel))
                _nailCategoryCollection = value
            End Set
        End Property

        Private _currentNailCategory As NailCategoryViewModel
        Public Property CurrentNailCategoryViewModel() As NailCategoryViewModel
            Get
                Return _currentNailCategory
            End Get
            Set(ByVal value As NailCategoryViewModel)
                _currentNailCategory = value
            End Set
        End Property

#End Region

#Region "Functions"

        ' TODO: use the LoadChildren to populate the nail-category tree
        Private Sub PopulateNailCategoryTree()
            ' Testing Data

            Dim categoryVMList As List(Of NailCategoryViewModel) = _
                (From category In New NaiCategoryGenerator().DataContainer _
                 Select New NailCategoryViewModel(category, Nothing, Me.Repository)).ToList()

            For Each categoryVM In categoryVMList
                AddHandler categoryVM.ChildNodeAdded, AddressOf Me.SubCategory_OnChildrenAdded
                AddHandler categoryVM.ChildNodeRenamed, AddressOf Me.SubCategory_OnRenamed
                AddHandler categoryVM.ChildNodeRemoved, AddressOf Me.SubCategory_OnRemoved
            Next

            Me.RootNailCategoryCollection = New ObservableCollection(Of NailCategoryViewModel)(categoryVMList)
        End Sub

#End Region

#Region "Event Handlers"

        Private Sub NailCategoryRepository_Added(ByVal sender As Object, ByVal e As DataEventArgs(Of NailCategory))
            ''Dim nailCategoryVM As New NailCategoryViewModel(e.Item, Me.Repository)
            ''Me.NailCategoryCollection.Add(nailCategoryVM)
        End Sub

        Private Sub NailCategoryRepository_Removed(ByVal sender As Object, ByVal e As DataEventArgs(Of NailCategory))

        End Sub

        Private Sub NailCategoryRepository_Updated(ByVal sender As Object, ByVal e As DataUpdatedEventArgs(Of NailCategory))

        End Sub

        Private Sub NailCategoryCollection_Changed(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
            If e.NewItems IsNot Nothing AndAlso e.NewItems.Count > 0 Then
                For Each vm As NailCategoryViewModel In Me.RootNailCategoryCollection
                    AddHandler vm.PropertyChanged, AddressOf Me.NailCategoryViewModel_PropertyChanged
                Next
            End If

            If e.OldItems IsNot Nothing AndAlso e.OldItems.Count > 0 Then
                For Each vm As NailCategoryViewModel In Me.RootNailCategoryCollection
                    RemoveHandler vm.PropertyChanged, AddressOf Me.NailCategoryViewModel_PropertyChanged
                Next
            End If
        End Sub

        Private Sub NailCategoryViewModel_PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            Dim IsSelected As String = "IsSelected"

            ' Make sure that the property name we're referencing is valid.
            ' This is a debugging technique, and does not execute in a Release build.
            TryCast(sender, NailServiceViewModel).VerifyPropertyName(IsSelected)

            ' When a customer is selected or unselected, we must let the
            ' world know that the TotalSelectedSales property has changed,
            ' so that it will be queried again for a new value.
            If e.PropertyName = IsSelected Then
                Me.OnPropertyChanged("TotalSelectedSales")
            End If
        End Sub

#End Region

#Region "Sub-Category Event Handlers"

        Private Sub SubCategory_OnRenamed(ByVal sender As Object, ByVal e As TreeNodeEventArgs(Of NailCategory))
            Dim renamedCategory As NailCategoryViewModel = DirectCast(sender, NailCategoryViewModel)
            Dim parent As NailCategoryViewModel = DirectCast(renamedCategory.Parent, NailCategoryViewModel)

            Dim lastIndex As Integer = parent.Children.IndexOf(renamedCategory)

            parent.Children.RemoveAt(lastIndex)
            parent.Children.Insert(lastIndex, renamedCategory)
        End Sub

        Private Sub SubCategory_OnRemoved(ByVal sender As Object, ByVal e As TreeNodeEventArgs(Of NailCategory))
            Dim removedCategory As NailCategoryViewModel = DirectCast(sender, NailCategoryViewModel)
            Dim parent As NailCategoryViewModel = DirectCast(removedCategory.Parent, NailCategoryViewModel)
            If parent IsNot Nothing Then
                parent.Children.Remove(removedCategory)
            End If
        End Sub

        Private Sub SubCategory_OnChildrenAdded(ByVal sender As Object, ByVal e As TreeNodeEventArgs(Of NailCategory))
            Dim parent As NailCategoryViewModel = DirectCast(sender, NailCategoryViewModel)
            If parent.HasMockChild Then
                parent.Children.Remove(NailCategoryViewModel.MockChild)
            End If

            Dim subCategoryVM As New NailCategoryViewModel(e.Item, parent, Me._repository)
            AddHandler subCategoryVM.ChildNodeAdded, AddressOf Me.SubCategory_OnChildrenAdded
            AddHandler subCategoryVM.ChildNodeRenamed, AddressOf Me.SubCategory_OnRenamed
            AddHandler subCategoryVM.ChildNodeRemoved, AddressOf Me.SubCategory_OnRemoved

            parent.Children.Add(subCategoryVM)
        End Sub

#End Region

		Protected Overrides Sub OnDispose()
            ''For Each vm In Me.NailCategoryCollection
            ''	vm.Dispose()
            ''Next

            ''Me.NailCategoryCollection.Clear()
            ''Me.CurrentNailCategoryViewModel.Dispose()

            ''RemoveHandler Me.NailCategoryCollection.CollectionChanged, AddressOf Me.NailCategoryCollection_Changed
            ''RemoveHandler _repository.DataAdded, AddressOf Me.NailCategoryRepository_Added
            ''RemoveHandler _repository.DataUpdated, AddressOf Me.NailCategoryRepository_Updated
            ''RemoveHandler _repository.DataUpdated, AddressOf Me.NailCategoryRepository_Updated
		End Sub
		
	End Class
End Namespace
